package com.fastMq.service.impl;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.fastMq.model.User;
import com.fastMq.service.PushService;

@Service("userPushService")  
public class UserPushServiceImpl implements PushService {  
  
      
    @Autowired
    @Qualifier("internalJmsTemplate")  
    private JmsTemplate jmsTemplate;  
      
    /** 
     * 这里是根据MQ配置文件定义的queue来注入的，也就是这里将会把不同的内容推送到不同的queue中 
     */  	
    @Autowired  
    @Qualifier("userServiceQueue")  
    private Destination destination;  
      
    public void push(final Object info) {  
        pushExecutor.execute(new Runnable() {  
            public void run() {  
                jmsTemplate.send(destination, new MessageCreator() {  
                    public Message createMessage(Session session) throws JMSException {  
                         User p = (User) info;
                         System.out.println(p);
                         return session.createTextMessage(JSON.toJSONString(p));  
                    }  
                });  
            }             
        });  
    }  
  
}  